package com.yuji.aacar.widgets.bigpic;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yuji.aacar.R;

import java.util.ArrayList;
import java.util.List;


/**
 *<li>Package: {@link WBigPicView}
 *<li>Date: 2017/6/21 17:52
 *<li>Discription: 大图浏览控件
 */

public class WBigPicView extends FrameLayout implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private ScrollView content_sv;
    private TextView content_tv;//具体描述
    private TextView title_tv;//描述的标题
    private TextView count_tv;//显示当前第几张图片

    private List<BigPicBean> been = new ArrayList<>();//数据源

    public WBigPicView(@NonNull Context context) {
        super(context);
        init();
    }

    public WBigPicView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WBigPicView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_big_pic, this);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        content_sv = (ScrollView) view.findViewById(R.id.big_pic_sv);
        content_tv = (TextView) view.findViewById(R.id.big_pic_content);
        title_tv = (TextView) view.findViewById(R.id.big_pic_title);
        count_tv = (TextView) view.findViewById(R.id.big_pic_count);
    }

    /**
     * 设置数据
     * @param been 数据源
     * @param onLoadImageListener 图片加载监听
     */
    public void setData(List<BigPicBean> been, BigPicPagerAdapter.OnLoadImageListener onLoadImageListener) {
        if (been != null) {
            this.been = been;
            BigPicPagerAdapter adapter = new BigPicPagerAdapter(been, LayoutInflater.from(getContext()));
            adapter.setOnLoadImageListener(onLoadImageListener);
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(this);
            for (int i = 0; i < been.size(); i++) {
                if (been.get(i).isCurrentItem()) {
                    setContent(i);
                    viewPager.setCurrentItem(i);
                    break;
                }
            }
        }
    }

    /**
     * 隐藏或显示底部描述信息
     * @param view
     * @param isShow
     */
    private void showOrHideView(View view, boolean isShow) {
        if (isShow) {
            view.setVisibility(VISIBLE);
        } else {
            view.setVisibility(GONE);
        }
    }

    /**
     * 设置底部描述信息
     * @param position
     */
    private void setContent(int position) {
        content_tv.setText(been.get(position).getContent());
        title_tv.setText(been.get(position).getTitle());
        count_tv.setText(position + 1 + "/" + been.size());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        showOrHideView(content_sv, been.get(position).isShowContent());
        setContent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public static class BigPicPagerAdapter extends PagerAdapter {
        private List<BigPicBean> been;
        private LayoutInflater inflater;
        private OnLoadImageListener onLoadImageListener;

        public BigPicPagerAdapter(List<BigPicBean> been, LayoutInflater inflater) {
            this.been = been;
            this.inflater = inflater;
        }

        @Override
        public int getCount() {
            return been.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            FrameLayout frameLayout = (FrameLayout) inflater.inflate(R.layout.item_big_pic, null);
//            PhotoView photoView = (PhotoView) frameLayout.findViewById(R.id.big_pic_photoview);
//            ProgressBar progressBar = (ProgressBar) frameLayout.findViewById(R.id.big_pic_progress);
//            container.addView(frameLayout);
//            String url = been.get(position).getUrl();
//            if (onLoadImageListener != null) {
//                progressBar.setVisibility(VISIBLE);
//                onLoadImageListener.onLoadImage(photoView, progressBar, url);
//            }
//            photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
//                @Override
//                public void onPhotoTap(ImageView view, float x, float y) {
////                    if (!TextUtils.isEmpty(content)) {
////                        dealView(sv_content);
////                    }
////                    if (isShow == true) {
////                        isShow = false;
////                    } else {
////                        isShow = true;
////                    }
//                }
//            });
            return frameLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        public void setOnLoadImageListener(OnLoadImageListener onLoadImageListener) {
            this.onLoadImageListener = onLoadImageListener;
        }

        public interface OnLoadImageListener {
            void onLoadImage(ImageView imageView, ProgressBar progressBar, String url);
        }
    }
}
